# more or less copied from MTGestureWidget

#__all__ = ['MTTouchAndHoldWidget'] # register for some reason???

from pymt import *
import threading
import math
import time

class MTTouchAndHoldWidget(MTWidget):

    def __init__(self, **kwargs):
      super(MTTouchAndHoldWidget, self).__init__(**kwargs)
      self.hold_distance = 10
      self.hold_time = 1.5
      self.touches = {}
      #print "MTTouchAndHoldWidget constructed"
    
    #def touch_still_down(self, timeDelta, touch=None):
     # print "touch=", touch
      #pass
      #if(touch.id in self.touches.keys()):
        #print "Touch still down"
#      self.touch_timers[touch.id].held = True
#      self.touch_timers[touch.id].cancel()
      #print "MTTouchAndHoldWidget dispatching on_touch_and_hold event"
      #self.dispatch_event('on_touch_and_hold', touch)
        
    def on_touch_down(self, touch): 
      #print "MTTouchAndHoldWidget::on_touch_down"
      touch.original_pos = touch.pos
      self.touches[touch.id] = touch
      #getClock().schedule_once(self.touch_still_down, 1.5, touch=touch)
      #getClock().schedule_once(curry(self.touch_still_down, 1.5, touch=touch))
      #self.touch_timers[touch.id] = threading.Timer(1.5, self.touch_still_down)
#      self.touch_timers[touch.id].held = False
#      self.touch_timers[touch.id].origin_x = touch.x
#      self.touch_timers[touch.id].origin_y = touch.y
      #self.touch_timers[touch.id].start()
      #return super(MTTouchAndHoldWidget, self).on_touch_down(touch)

    def on_touch_move(self, touch):
      pass
#      if(touch.id in self.touch_timers.keys()):
#        if(self.touch_moved_too_far(touch) == True):
#          #print "MTTouchAndHoldWidget cancel the timer"
#          self.touch_timers[touch.id].cancel()
#          # forget about this touch now it's been moved
#          del self.touch_timers[touch.id]
#          #self.on_touch_down(touch) # create a new touch
#      #return super(MTTouchAndHoldWidget, self).on_touch_move(touch)# MAYBE THESE SHOULD BE RETURN TRUE OF FALSE
      
    def on_touch_up(self, touch):
      if(touch.id in self.touches.keys()):
        del self.touches[touch.id]
#      if(touch.id in self.touch_timers.keys()):
#        #print "MTTouchAndHoldWidget::on_touch_up"
#        if(self.touch_timers[touch.id].held == True): # if the touch was held long enough 
#          #print "MTTouchAndHoldWidget dispatching on_touch_and_hold_release event"
#          self.dispatch_event('on_touch_and_hold_release', touch)
#          #self.dispatch_event('on_touch_and_hold', touch)
#          
#        #print "MTTouchAndHoldWidget cancel the timer"
#        self.touch_timers[touch.id].cancel()
#          
#        # forget about this touch now it's been dispatched
#        del self.touch_timers[touch.id]
#        
#      #return super(MTTouchAndHoldWidget, self).on_touch_up(touch)
        
    def touch_moved_too_far(self, touch):
      #print "MTTouchAndHoldWidget::touch_moved_too_far. touch now:", touch.x, touch.y, "touch before: ", self.touch_timers[touch.id].origin_x, self.touch_timers[touch.id].origin_y
      # check moved only a small distance
      return((math.fabs(touch.x - self.touch_timers[touch.id].origin_x) > 10) or (math.fabs(touch.y - self.touch_timers[touch.id].origin_y) > 10))
    
    #def on_touch_and_hold(self, touch):
   #   pass
        
# Register all base widgets
MTWidgetFactory.register('MTTouchAndHoldWidget', MTTouchAndHoldWidget)
